﻿#include"Sort.h"
#include"Stack.h"
//交换函数
void Swap(int* x, int* y)
{
	int tmp = *x;
	*x = *y;
	*y = tmp;
}

//直接插入排序
void InsertSort(int* arr, int n)
{
	for (int i = 0; i < n - 1; ++i)
	{
		int end = i;
		int tmp = arr[end+1];
		while (end >= 0 && arr[end] > tmp)
		{
			arr[end + 1] = arr[end];
			end--;
		}
		arr[end + 1] = tmp;
	}
}

//希尔排序
void ShellSort(int* arr, int n)
{
	int gap = n;

	while (gap > 1)
	{
		//推荐写法
		gap = gap / 3 + 1;
		for (int i = 0; i < n - gap; ++i)
		{
			int end = i;
			int tmp = arr[end + gap];
			while (end >= 0 && arr[end] > tmp)
			{
				arr[end + gap] = arr[end];
				end -= gap;
			}

			arr[end + gap] = tmp;
		}
	}
}

//直接选择排序
void SelectSort(int* arr, int n)
{
	int begin = 0; int end = n - 1;
	while (begin < end)
	{
		int mini = begin, maxi = begin;
		for (int i = begin; i <= end; ++i)
		{
			if (arr[i] > arr[maxi])
			{
				maxi = i;
			}
			if (arr[i] < arr[mini])
			{
				mini = i;
			}
		}
		if (maxi == begin)
		{
			maxi = mini;
		}
		Swap(&arr[begin],&arr[mini]);
		Swap(&arr[end], &arr[maxi]);
		++begin;
		--end;
	}
}

//向下调整法
void AdjustDown(int* arr, int parent, int n)
{
	int child = parent * 2 + 1; 
	while (child < n)
	{
		//建立小堆降序 <			>
		//建立大堆升序 <			<
		if (child + 1 < n && arr[child] < arr[child + 1])
		{
			++child;
		}
		//建立小堆降序 <			
		//建立大堆升序 >		
		if (arr[child] > arr[parent])
		{
			Swap(&arr[child], &arr[parent]);
			parent = child;
			child = parent * 2 + 1;
		}
		else
		{
			break;
		}
	}
}
//堆排序
void HeapSort(int* arr, int n)
{
	//建立大堆
	for (int i = (n - 1 - 1) / 2; i >= 0; --i)
	{
		AdjustDown(arr, i,n);
	}
	int end = n - 1;
	while (end > 0)
	{
		Swap(&arr[0], &arr[end]);
		AdjustDown(arr, 0, end);
		--end;
	}
}

//冒泡排序
void BubbleSort(int* arr, int n)
{
	for (int i = 0; i < n - 1; ++i)
	{
		int flag = 1;
		for (int j = 0; j < n - 1 - i; ++j)
		{	
			if (arr[j] > arr[j + 1])
			{
				Swap(&arr[j], &arr[j + 1]);
				flag = 0;
			}
		}
		if (flag)
		{
			break;
		}
	}
}

//找基准值
//hoare版本
int  _QuickSort1(int* arr, int left, int right)
{
	int key = left;
	left++;
	while (left <= right)
	{
		while (left <= right && arr[right] > arr[key])
		{
			//找比基准值小的
			right--;
		}
		while (left <= right && arr[left] < arr[key])
		{
			//找比基准值大的
			left++;
		}
		if (left <= right)
		{
			Swap(&arr[left++], &arr[right--]);
		}
	}
	Swap(&arr[right], &arr[key]);
	return right;
}
//挖坑法
int _QuickSort2(int* arr, int left, int right)
{
	int hole = left;
	int key = arr[hole];
	while (left < right)
	{
		while (left < right && arr[right] >= key)
		{
			//找比基准值小的
			right--;
		}
		arr[hole] = arr[right];
		hole = right;
		while (left < right && arr[left] <= key)
		{
			//找比基准值大的
			left++;
		}
		arr[hole] = arr[left];
		hole = left;
		
	}
	arr[hole] = key;
	return hole; 
}



//lomuto前后指针
int _QuickSort3(int* arr, int left, int right)
{
	int prev = left, cur = left+1;
	int key = left;
	while (cur <= right)
	{
		if (arr[cur] < arr[key] && ++prev != cur)
		{
			Swap(&arr[prev], &arr[cur]);
		}
		++cur;
	}
	Swap(&arr[prev], &arr[key]);
	return prev;
}
//快速排序
void QuickSort(int* arr, int left, int right)
{
	if (left >= right)
	{
		return;
	}
	//_QuickSort⽤于按照基准值将区间[left,right)中的元素进⾏划分
	int key = _QuickSort2(arr, left, right);
	QuickSort(arr,left, key - 1);
	QuickSort(arr, key + 1, right);
}

//快速排序三路划分
void QuickSort33(int* arr, int left, int right)
{
	if (left >= right)
	{
		return;
	}
	int begin = left;
	int end = right;
	int cur = left + 1;
	int key = arr[left];
	while (cur <= right)
	{
		if (arr[cur] < key)
		{
			Swap(&arr[left], &arr[cur]);
			left++;
			cur++;
		}
		else if (arr[cur] > key)
		{
			Swap(&arr[right], &arr[cur]);
			right--;
		
		}
		else
		{
			cur++;
		}

	}
	QuickSort33(arr, begin, left - 1);
	QuickSort33(arr, right+1, end);

}

//快速排序非递归实现
void QuickSortNonR(int* arr, int left, int right)
{
	 //创建一个栈
	Stack st;
	StackInit(&st);
	//先放右边再放左边
	StackPush(&st, right);
	StackPush(&st, left);
	while (!StackEmpty(&st))
	{
		//取栈顶两次
		int begin = StackTop(&st);
		StackPop(&st);
		int end = StackTop(&st);
		StackPop(&st);
		//[begin,end]
		//找基准值
		int key = begin;
		int prev = begin;
		int cur = prev + 1;
		//双指针法
		while (cur <= end)
		{
			if (arr[cur] < arr[key] && ++prev != cur)
			{
				Swap(&arr[prev], &arr[cur]);
			}
			++cur;
		}
		Swap(&arr[prev], &arr[key]);
		key = prev;
		//右边[key+1,end];
		//左边[begin,key-1];
		if (key + 1 < end) //只有一个元素和左边大于右边不用入栈
		{
			StackPush(&st,end);
			StackPush(&st,key+1);
		}
		if (begin < key - 1)
		{
			StackPush(&st, key - 1);
			StackPush(&st, begin);
		}
	}

	StackDestroy(&st);
}
void _MergeSort(int* arr, int left, int right,int* tmp)
{
	//分解
	if (left >= right) //只有一个元素终止
	{
		return;
	}
	int mid = left + (right - left) / 2; //==(left + right) / 2
	//根据mid将[left,right]划分左右两个序列;[left,mid] [mid+1,right]
	_MergeSort(arr, left, mid,tmp);
	_MergeSort(arr, mid + 1, right,tmp);

	//合并  [left,mid] [mid+1,right]
	int begin1 = left, end1 = mid;
	int begin2 = mid + 1, end2 = right;
	int index = left;
	while (begin1 <= end1 &&begin2<=end2)
	{
		if (arr[begin1] < arr[begin2])
		{
			tmp[index++] = arr[begin1++];
		}
		else
		{
			tmp[index++] = arr[begin2++];
		}
	}
	while (begin1 <= end1)
	{
		tmp[index++] = arr[begin1++];
	}
	while(begin2 <= end2)
	{
		tmp[index++] = arr[begin2++];
	}
	//把数据拷回去
	for (int i = left; i <= right; ++i)
	{
		arr[i] = tmp[i];
	}
}
//归并排序
void MergeSort(int* arr, int n)
{
	int* tmp = (int*)malloc(sizeof(int) * n);
	if (tmp == NULL)
	{
		perror("malloc fail!");
		exit(1);
	}
	_MergeSort(arr, 0, n - 1,tmp);
	free(tmp);
}

//非递归归并排序
void MergeSortNonR(int* arr, int n)
{
	int* tmp = (int*)malloc(sizeof(int) * n);
	if (tmp == NULL)
	{
		perror("malloc fail\n");
		exit(1);
	}
	int gap = 1; //每组有gap个数据
	while (gap<n)
	{
		//根据gap划分组，两两一合并
		for (int i = 0; i < n;i+=2*gap)
		{
			int begin1 = i,end1=i+gap-1;
			int begin2 = i + gap, end2 = i + 2 * gap - 1;
			if (begin2 >= n)
			{
				//越界了直接跳过本次合并
				break;
			}
			if (end2 >= n)
			{
				end2 = n - 1;
			}
			//两个有序序列进行合并
			int index = begin1;
			while (begin1 <= end1 && begin2 <= end2)
			{
				if (arr[begin1] < arr[begin2])
				{
					tmp[index++] = arr[begin1++];
				}
				else
				{
					tmp[index++] = arr[begin2++];
				}
			}
			while (begin1 <= end1)
			{
				tmp[index++] = arr[begin1++];
			}
			while (begin2 <= end2)
			{
				tmp[index++] = arr[begin2++];
			}
			//导入到原数组中
			memcpy(arr + i, tmp + i, sizeof(int) * (end2 - i + 1));
		}
		gap *= 2;
	}
	free(tmp);
}

//计数排序
void CountSort(int* arr, int n)
{
	int mini = arr[0], maxi = arr[0];
	//找最大值和最小值
	for (int i = 1; i < n; ++i)
	{
		if (arr[i] > maxi)
		{
			maxi = arr[i];
		}
		if (arr[i] < mini)
		{
			mini = arr[i];
		}
	}
	//范围
	int range = maxi - mini + 1;
	int* count = (int*)malloc(sizeof(int) * range);
	if (count == NULL)
	{
		perror("malloc fail");
		exit(1);
	}
	//对count初始化 calloc  或 memeset
	memset(count, 0, sizeof(int) * range);
	//统计原数组个数映射到新count数组中
	for (int i = 0; i < n; ++i)
	{
		count[arr[i] - mini]++;
	}
	int index = 0;
	//将count数组还原到原数组
	for (int i = 0; i < range; ++i)
	{
		while (count[i]--)
		{	//还原
			arr[index++] = i + mini;
		}
	}
	free(count);
	
}
